package com.yglab.sqpoll.android.settings;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.view.Window;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.app.SherlockPreferenceActivity;

import com.yglab.sqpoll.android.R;
import com.yglab.sqpoll.android.login.LoginFacebook;
import com.yglab.sqpoll.android.splash.SplashActivity;

/**
 * The activity for Settings
 * 
 * @author HongGyun Bae
 */
public class SettingsActivity extends SherlockPreferenceActivity {
	private final int DIALOG_LOGOUT = 1;	
		
	Preference mPref_Profile;
	Preference mPref_Logout;
	Preference mPref_Version;
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
				
		addPreferencesFromResource(R.xml.settings);
		startActivity(new Intent(this, SplashActivity.class));
		//Going to ProfileActivity
		mPref_Profile = (Preference)findPreference("pref_profile");
		Intent p_intent = new Intent(getBaseContext(),ProfileActivity.class);
		mPref_Profile.setIntent(p_intent);
				
		//Going to VersionActivity
		mPref_Version = (Preference)findPreference("pref_version");
		Intent v_intent = new Intent(getBaseContext(),VersionActivity.class);
		mPref_Version.setIntent(v_intent);
		
		//Doing logout
		mPref_Logout = (Preference)findPreference("pref_logout");
		mPref_Logout.setOnPreferenceClickListener(new OnPreferenceClickListener(){
			
			@Override
			public boolean onPreferenceClick(Preference preference) {
				// TODO Auto-generated method stub
				
				if(preference.getKey().equals("pref_logout")){
					showDialog(DIALOG_LOGOUT);
				}
				return false;
			}
		});
		
	}
	

	/**
	 * dialog box of logout
	 */
	protected Dialog onCreateDialog(int id){
		switch(id){
		case DIALOG_LOGOUT:
			Builder bld = new AlertDialog.Builder(this);
			bld.setTitle("info");
			bld.setMessage(getResources().getString(R.string.logout_check));
			
			bld.setPositiveButton("OK", new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int whichButton){
					Toast.makeText(getBaseContext(), "Logout", Toast.LENGTH_LONG).show();
					
					Intent log_intent = new Intent(getBaseContext(),LoginFacebook.class);
					startActivity(log_intent);
				}
			});
			
			bld.setNegativeButton("Cancel", null);
			
			AlertDialog alertD = bld.create();
			return alertD;
		}
		return null;
	}
}
